package com.feonix.common.util.mvc;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.feonix.common.util.DateUtil;
import com.feonix.common.util.StringUtil;


public class MvcDateConverter extends PropertyEditorSupport {
	
	protected final Log logger = LogFactory.getLog(super.getClass());

	private String[] dateFormates = null;

	public MvcDateConverter(String[] dateFormates) {
		this.dateFormates = dateFormates;
	}

	public void setAsText(String paramString) throws IllegalArgumentException {
		if (StringUtil.isEmpty(paramString)) {
			setValue(null);
			return;
		}
		Date date = null;
		try {
			date = DateUtils.parseDate(paramString, this.dateFormates);
		} catch (Exception exception) {
			this.logger.error("parseDate error", exception);
		}
		setValue(date);
	}

	public String getAsText() {
		Date date = (Date) getValue();
		String str = "";
		if (date != null) {
			str = DateUtil.dateToString(date, "yyyy-MM-dd HH:mm:ss");
		}
		return str;
	}

}
